package com.hyt.it.ogt.kq.service.gov.service.impl;

import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.common.gov.constant.CodeEnum;
import com.hyt.it.ogt.kq.service.gov.mapper.TaskPlaceMapper;
import com.hyt.it.ogt.kq.service.gov.model.dto.PlaceAreaInfoDTO;
import com.hyt.it.ogt.kq.service.gov.model.dto.PlaceRoomInfoDTO;
import com.hyt.it.ogt.kq.service.gov.model.entity.TaskPlace;
import com.hyt.it.ogt.kq.service.gov.model.vo.TaskPlaceVO;
import com.hyt.it.ogt.kq.service.gov.model.xml.examsys.gce.RegisterInfoGceResult;
import com.hyt.it.ogt.kq.service.gov.service.ITaskPlaceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 任务考点表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2021-04-12
 */
@Service
public class TaskPlaceServiceImpl extends BaseServiceImpl<TaskPlaceMapper, TaskPlace> implements ITaskPlaceService {

    @Resource
    private TaskAreaServiceImpl taskAreaService;


    @Override
    public List<TaskPlace> getTaskPlace(String taskId, String districtId) {
        return lambdaQuery()
                .eq(TaskPlace::getTaskId,taskId)
                .eq(TaskPlace::getDistrict,districtId)
                .eq(TaskPlace::getDelFlag,false)
                .orderByAsc(TaskPlace::getCode)
                .list();
    }

    @Override
    public RegisterInfoGceResult getGceRegisterPlaceInfo(String taskCode, String code) {
        return baseMapper.getGceRegisterPlaceInfo(taskCode, code);
    }

    @Override
    public PlaceRoomInfoDTO getPlaceRoomInfoByCode(String taskCode, String roomCode) {
        return baseMapper.getPlaceRoomInfoByCode(taskCode, roomCode);
    }

    @Override
    public List<TaskPlace> listByTaskId(String taskId) {
        return lambdaQuery()
                .eq(TaskPlace::getTaskId,taskId)
                .eq(TaskPlace::getDelFlag,false)
                .list();
    }

    @Override
    public Map<String,TaskPlace> mapByTaskId(String taskId) {
        List<TaskPlace> taskPlaceList =  this.listByTaskId(taskId);
        Map<String,TaskPlace> map = new HashMap<>();
        taskPlaceList.stream().forEach(taskPlace -> {
            map.put(taskPlace.getId(),taskPlace);
        });

        return map;
    }

    @Override
    public TaskPlace getOneTaskPlace(String taskId) {
        return this.lambdaQuery()
                .eq(TaskPlace::getTaskId, taskId)
                .eq(TaskPlace::getDelFlag, false)
                .orderByAsc(TaskPlace::getCode)
                .last("LIMIT 1")
                .one();
    }

    @Override
    public int countTaskCity(String taskId) {
        return baseMapper.countTaskCity(taskId);
    }

    @Override
    public PlaceAreaInfoDTO getPlaceAreaInfo(String placeId) {
        return baseMapper.getPlaceAreaInfo(placeId);
    }

    @Override
    public void initVirtualTaskPlaceRandomDistrict(String taskId) {

        //判断考点是否存在
        Integer count = lambdaQuery().eq(TaskPlace::getTaskId,taskId)
                .eq(TaskPlace::getDelFlag,false)
                .count();
        if (count > 0) {
            return;
        }
        //随机获取一个省市县去
        String districtId = taskAreaService.randomGetOneDistrictId(taskId);
        Map<String,String> map = taskAreaService.taskAreaMap(taskId);
        String cityId = map.get(districtId);
        String provinceId = map.get(cityId);

        //初始化虚拟考点
        TaskPlace taskPlace = new TaskPlace();
        taskPlace.setTaskId(taskId)
                .setCode(1)
                .setName("虚拟考点")
                .setProvince(provinceId)
                .setCity(cityId)
                .setDistrict(districtId);
        this.save(taskPlace);
    }

    @Override
    public TaskPlace oneCustomPlace(String taskId) {
        return this.lambdaQuery()
                .eq(TaskPlace::getTaskId, taskId)
                .eq(TaskPlace::getFunction, CodeEnum.PLACE_FUNCTION_CUSTOM.getCode())
                .eq(TaskPlace::getDelFlag, Boolean.FALSE)
                .last("LIMIT 1")
                .one();
    }

    @Override
    public List<TaskPlaceVO> getTaskPlaceList(String taskId) {
        return baseMapper.getTaskPlaceList(taskId);
    }

    @Override
    public TaskPlace getTaskByRoomId(String taskId, String roomId) {
        return baseMapper.getTaskByRoomId(taskId, roomId);
    }
}
